package system;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import oracle.stellent.ridc.IdcClient;
import oracle.stellent.ridc.IdcClientException;
import oracle.stellent.ridc.IdcClientManager;
import oracle.stellent.ridc.IdcContext;
import oracle.stellent.ridc.model.DataBinder;
import oracle.stellent.ridc.model.DataObject;
import oracle.stellent.ridc.model.DataResultSet;
import oracle.stellent.ridc.protocol.ServiceResponse;

public class ManageWebCenterContent {
    private static String hostname = "";
    private static String username = "";
    private static String password = "";

    private DataBinder binder;
    private IdcClient idcClient;
    private IdcContext userContext;

    public ManageWebCenterContent() throws IOException, IdcClientException {
        super();
        readProperties();
        createConnection();
    }

    private void createConnection() throws IdcClientException, IOException {
        IdcClientManager manager = new IdcClientManager();
        // build a client that will communicate using the intradoc protocol
        //this protocol requires authentication credentials for each request
        //IdcClient idcClient = manager.createClient("idc://192.168.50.56:4444");
        // build a client that will communicate using the HTTP protocol
        idcClient = manager.createClient(hostname);
        idcClient.getConfig().setProperty("http.library", "apache4");
        idcClient.getConfig().setSocketTimeout(30000); // 30 seconds
        idcClient.getConfig().setConnectionSize(1); // 1 connections
        userContext = new IdcContext(username, password);
        binder = idcClient.createBinder();
    }

    public List<Map<String, Object>> docHistory(String where,
                                                String order) throws IdcClientException {
        List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
        
        // populate the binder with the parameters
        binder.putLocal("IdcService", "GET_DATARESULTSET");
        binder.putLocal("dataSource", "DocHistory");
        binder.putLocal("whereClause", where);
        binder.putLocal("orderClause", order);
        binder.putLocal("resultName", "DOC_HISTORY");
        binder.putLocal("UserTimeZone", "Etc/GMT+7");
        ServiceResponse resp = idcClient.sendRequest(userContext, binder);
        idcClient.sendRequest(userContext, binder);
        // get the binder
        binder = resp.getResponseAsBinder();
        DataResultSet rs = binder.getResultSet("DOC_HISTORY");
        
        if (rs != null && rs.getRows().size() > 0) {
            // loop over the results
            for (DataObject dataObject : rs.getRows()) {
                Map<String, Object> lstTemp = new HashMap<String, Object>();
                String dId = dataObject.get("dID");
                String dDocName = dataObject.get("dDocName");
                Map<String, Object> contentInfo = null;
                try {
                    contentInfo = getContentInformation(dId, dDocName);
                } catch(Exception ex){
                    lstTemp.put("dDocName" + " - Đã xóa", dataObject.get("dDocName"));
                }
                if(contentInfo != null){
                    lstTemp.put("dDocName", contentInfo.get("dDocTitle"));
                }
                lstTemp.put("dAction", dataObject.get("dAction"));
                lstTemp.put("dActionDate", dataObject.get("dActionDate"));
                lstTemp.put("dUser", dataObject.get("dUser"));
                //lstTemp.put("dAction", dataObject.get("dAction"));
                //lstTemp.put("dAction", dataObject.get("dAction"));
                //lstTemp.put("dAction", dataObject.get("dAction"));
                lst.add(lstTemp);
            }
        }

        return lst;
    }
    
    public Map<String, Object> getContentInformation(String dId, String dDocName) throws IdcClientException {
        // populate the binder with the parameters
        binder.putLocal("IdcService", "DOC_INFO");
        //binder.putLocal("dID", "QdocInfo");
        binder.putLocal("dID", dId);
        binder.putLocal("dDocName", dDocName);
        
        binder.putLocal("resultName", "DOC_INFO");
        ServiceResponse resp = idcClient.sendRequest(userContext, binder);
        idcClient.sendRequest(userContext, binder);
        // get the binder
        binder = resp.getResponseAsBinder();
        DataResultSet rs = binder.getResultSet("DOC_INFO");
        
        Map<String, Object> lstTemp = new HashMap<String, Object>();
        
        if (rs != null && rs.getRows().size() > 0) {
            for (DataObject dataObject : rs.getRows()) {
                lstTemp.put("dDocTitle", dataObject.get("dDocTitle"));
            }
        }
        
        return lstTemp;
    }
    
    public String getLoaiNoiDung(){
        return "";
    }
    
    public String getChuyenMuc(){
        return "";
    }
    
    public String getMucHienThi(){
        return "";
    }

    private void readProperties() throws IOException {
        Properties props = new Properties();
        InputStream in =
            getClass().getResourceAsStream("WebCenterContent.properties");
        props.load(in);
        in.close();
        hostname = props.getProperty("hostname");
        username = props.getProperty("username");
        password = props.getProperty("password");
    }
}
